package com.xcc.mota.screen;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.RectF;

import com.xcc.mota.GameView;
import com.xcc.mota.event.TouchEvent;
import com.xcc.mota.sprite.BaseSprite;
import com.xcc.mota.utils.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 场景父类
 * Created by Administrator on 2018/1/8.
 */
public abstract class BaseScreen {
    public static final int ScreenWidth = GameView.ScreenWidth;
    public static final int ScreenHeight = GameView.ScreenHeight;
    protected Context context;
    protected OnScreenEvent onScreenEvent;
    private List<BaseSprite> touchSpriteList;

    public BaseScreen(Context context) {
        this.context = context;
        touchSpriteList = new ArrayList<>();
    }

    protected BaseScreen addTouchSprite(BaseSprite baseSprite) {
        touchSpriteList.add(baseSprite);
        return this;
    }

    public BaseScreen setOnScreenEvent(OnScreenEvent onScreenEvent) {
        this.onScreenEvent = onScreenEvent;
        return this;
    }

    public abstract void onDraw(Canvas canvas);

    public void onTouchEvent(TouchEvent event) {
        float x = event.getX();
        float y = event.getY();
        if (Logger.isDebug) {
            Logger.out("场景位置x:" + x);
            Logger.out("场景位置y:" + y);
        }
        for (int i = 0; i < touchSpriteList.size(); i++) {
            BaseSprite baseSprite = touchSpriteList.get(i);
            RectF rectF = baseSprite.getRectF();
            if (rectF == null) continue;
            else if (rectF.left < x && rectF.right > x && rectF.top < y && rectF.bottom > y) {
                baseSprite.onTouchEvent(event);
                break;
            }
        }
    }

    public interface OnScreenEvent {
        void onEvent(int type, int msg);
    }

    public static class ScreenEvent {
        public static final int TypeExitGame = 0;//退出游戏
        public static final int TypeOpenScreen = 1;//开启其他页面

        public static final int MsgMenu = 0;//去菜单页
        public static final int MsgAbout = 1;//去关于页
        public static final int MsgLevel1 = 2;//关卡1
    }
}
